package impl;

import java.util.ArrayList;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

//import api.IPostman;
import api.InvalidAuthenticationException;
import api.ejb.ISocializer;

import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import org.jboss.logging.Logger;

import p3_entities.Friend;
import p3_entities.Messageboard;
import p3_entities.Usermessage;
import p3_entities.Userphoto;
import p3_entities.Userprofile;


/**
 * Session Bean implementation class Socializer
 */
@Stateless(name = "ISocializer")
//@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public class Socializer implements ISocializer {
	@PersistenceContext(unitName = "PhaseBookPU")
	EntityManager em;

	private static final Logger log = Logger.getLogger(Socializer.class);

	
	static final long MILLIS_IN_A_DAY = 1000 * 60 * 60 * 24;

	/**
	 * Default constructor.
	 */
	public Socializer() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<String> searchFriends(String searchString) {
		log.info("Executing ---------->");

		Query query = em
				.createNativeQuery("SELECT Email FROM userprofiles WHERE name REGEXP '"
						+ searchString + "'");
		
		log.info("<--------------- Done");


		return query.getResultList();

	}

	@Override
	public boolean sendFriendshipInvitation(int fromId, int toId) {
		
		
		log.info("Executing ---------->");


		Friend friend = new Friend();
		Userprofile userprofile1 = em.find(Userprofile.class, fromId);
		Userprofile userprofile2 = em.find(Userprofile.class, toId);

		friend.setUserprofile1(userprofile1);
		friend.setUserprofile2(userprofile2);

		em.persist(friend);
		
		log.info("<--------------- Done");


		
		return true;

	}

	@Override
	public List<Friend> getPendingFriendshipRequests(int uid) {
		log.info("Executing ---------->");


		Query query = em.createNamedQuery("getPendingRequests");
		query.setParameter("userId", uid);
		
		log.info("<--------------- Done");


		return query.getResultList();

	}

	@Override
	public boolean confirmFriendshipRequest(int uid, int requesterdId) {
		log.info("Executing ---------->");


		Query query = em.createNamedQuery("getRequest");
		query.setParameter("userId1", requesterdId);
		query.setParameter("userId2", uid);

		Friend friend = (Friend) query.getSingleResult();

		friend.setRelationshipActive(true);

		em.persist(friend);

		Userprofile userprofile1 = em.find(Userprofile.class, requesterdId);
		Userprofile userprofile2 = em.find(Userprofile.class, uid);

		log.info("<--------------- Done");


		return true;

	}

	@Override
	public boolean isFriendWith(int baseId, int friendId) {
		log.info("Executing ---------->");


		Query query = em.createNamedQuery("isFriend");
		query.setParameter("userId1", baseId);
		query.setParameter("userId2", friendId);

		Integer id = (Integer) query.getSingleResult();

		if (id != null)
			return true;
		
		log.info("<--------------- Done");


		return false;

	}

	@Override
	public List<Userprofile> getMyFriends(int uid) {
		log.info("Executing ---------->");

		List<Userprofile> friendsList = new ArrayList<Userprofile>();

		Query query = em.createNamedQuery("myFriends");
		query.setParameter("userId", uid);

		List<Friend> friendships = (List<Friend>) query.getResultList();

		for (Friend friend : friendships) {
			if (friend.getUserprofile1().getUserId() == uid)
				friendsList.add(friend.getUserprofile2());
			else
				friendsList.add(friend.getUserprofile1());
		}

		log.info("<--------------- Done");

		return friendsList;
	}

	
}
